<template>
  <div style="padding-bottom: 50px">
    <!-- search-bar -->
    <van-search
      shape="round"
      background="#ba3627"
      placeholder="请输入搜索关键词"
    />
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <!-- 轮播图 -->
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="item in banners" :key="item.bannerid">
          <img :src="item.img" />
        </van-swipe-item>
      </van-swipe>
      <!-- 菜单列表 -->
      <van-grid :column-num="5" :border="false">
        <van-grid-item
          v-for="item in menus"
          :key="item.id"
          :icon="item.icon"
          :text="item.title"
        />
      </van-grid>
      <!-- 商品列表 -->
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getGoodsListData"
      >
        <div class="goods-list">
          <!-- 声明式导航 -->
          <!-- <router-link
            :to="'/detail?proid=' + item.proid"
            v-for="item in goodsList"
            :key="item.proid"
            class="goods-item"
          >
            <img :src="item.img1" @error="onError" alt="" />
            <div class="title">{{ item.proname }}</div>
            <div class="desc">{{ item.desc }}</div>
            <div class="price">￥{{ formatPrice(item.originprice) }}</div>
          </router-link> -->
          <!-- 编程式导航 -->
          <div
            v-for="item in goodsList"
            :key="item.proid"
            class="goods-item"
            @click="goToDetail(item.proid)"
          >
            <img :src="item.img1" @error="onError" alt="" />
            <div class="title">{{ item.proname }}</div>
            <div class="desc">{{ item.desc }}</div>
            <div class="price">￥{{ formatPrice(item.originprice) }}</div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script setup name="Home">
import { ref } from 'vue'
import { getBanners, getMenus, getGoodsList } from '@/api/home'
import { useRouter } from 'vue-router'
const router = useRouter()

// 定义模型数据
const banners = ref([])
// let menus = reactive([])
const menus = ref([])
const goodsList = ref([])
const loading = ref(false)
const finished = ref(false)
const refreshing = ref(false)
let count = 0

// 获取轮播图的方法
const getBannerListData = async () => {
  const res = await getBanners()
  banners.value = res.data
}

// 获取菜单列表的方法
const getMenusData = async () => {
  const res = await getMenus()
  menus.value = res.data
}

// 获取商品列表的方法
const getGoodsListData = async () => {
  count++
  const res = await getGoodsList(count)

  if (res.data.length === 0) {
    finished.value = true
    return
  }

  // 上拉加载更多
  goodsList.value = [...goodsList.value, ...res.data]

  // 这个每次加载数据结束之后，要把它的值，重置为false
  loading.value = false
  refreshing.value = false
}

const onRefresh = () => {
  // 清空之前的数据
  goodsList.value = []
  // 把页面设置为0
  count = 0
  // 把 loading 设置为 true
  loading.value = true

  getGoodsListData()
}

// Vue3中函数就代替了过滤器
const formatPrice = price => {
  return price.toFixed(2)
}

// 监听图片加载失败
const onError = e => {
  e.target.src =
    'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
}

// 跳转到商品详情页面
const goToDetail = proid => {
  router.push(`/detail?proid=${proid}`)
}

getBannerListData()
getMenusData()
// getGoodsListData()
</script>

<style lang="scss" scoped>
.my-swipe {
  width: 100%;
  height: 200px;
  img {
    width: 100%;
    height: 100%;
  }
}
.goods-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  box-sizing: border-box;
  width: 100%;

  .goods-item {
    box-sizing: border-box;
    background-color: #fff;
    padding: 5.33333vw;
    border-radius: 1.33333vw;
    display: flex;
    align-items: center;
    width: 48%;
    flex-direction: column;
    margin-top: 1.33333vw;
    img {
      width: 26.66667vw;
      height: 32vw;
    }
    .title {
      font-size: 3.73333vw;
      display: inline-block;
      white-space: nowrap;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-top: 4vw;
      color: #000;
    }
    .desc {
      font-size: 3.2vw;
      color: #c3c3c3;
      display: inline-block;
      white-space: nowrap;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-top: 2.66667vw;
    }
    .price {
      font-size: 3.2vw;
      margin-top: 2.66667vw;
      color: red;
    }
  }
}
// 深度作用选择器
::v-deep .van-list__finished-text {
  width: 100%;
  text-align: center;
}
</style>
